package com.woniuxy.house.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.entity.broker.Broker;
import com.woniuxy.entity.house.Comments;
import com.woniuxy.house.feign.BrokerService;
import com.woniuxy.house.mapper.CommentsMapper;
import com.woniuxy.house.service.CommentsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {

    @Resource
    private CommentsMapper commentsMapper;
    @Resource
    private BrokerService brokerService;

    /**
     * 通过楼盘id查询所有评论
     * @param id
     * @return
     */
    @Override
    public Page<Comments> findByBid(int id, int current, int size) {
        QueryWrapper<Comments> wrapper = new QueryWrapper<>();
        wrapper.eq("bid",id);
        return commentsMapper.selectPage(new Page<>(current,size),wrapper);
    }

    /**
     * 删除某条评论
     * @param id
     * @return
     */
    @Override
    public boolean deleteComments(int id) {
        Comments comments = new Comments();
        comments.setId(id);
        comments.setStatus(1);
        return commentsMapper.updateById(comments)>0;
    }

    /**
     * 新增评论
     * @param comments
     * @return
     */
    @Override
    public boolean insertComments(Comments comments) {
        //调用broker微服务获取经纪人信息
        ResponseResult<Broker> responseResult = brokerService.getSingleBroker(comments.getId());
        Broker broker = responseResult.getData();
        comments.setAgentImg(broker.getAvatar());
        comments.setAgentName(broker.getName());
        LocalDate date = LocalDate.now();
        comments.setTime(date);
        return commentsMapper.insert(comments)>0;
    }
}
